﻿<%@ WebHandler Language="C#" Class="danwei" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using System.Configuration;
using ServiceStack.Redis;
using System.Text;

public class danwei : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.QueryString["uid"];
            string kid = context.Request.QueryString["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("[]");
                return;
            }
           string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid,"cache").Replace(kid,"cache"));
            using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
            {
                if (redis != null)
                {
                    if (redis.ContainsKey(key))
                    {
                        context.Response.Write(redis.GetValue(key));
                        return;
                    }
                }
            }
            string danWeiLeiXing = context.Request.QueryString["danweileixing"];
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                string createCondition = m_DB.CreateAndCondition("danweileixing", DataValueCommonType.字符串, danWeiLeiXing, "", SearchConditionType.等于);
                DataTable Dt = m_DB.GetDataTableByCondition("tbl_danwei", createCondition);
                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append("[{\"danwei\":\"—全部—\",\"selected\":true}");
                foreach (DataRow row in Dt.Rows)
                {
                    strBuilder.Append(string.Format(",{0}\"danwei\":\"{1}\"{2}", "{", row["danweimingcheng"], "}"));
                }
                strBuilder.Append("]");
                string responseJson = strBuilder.ToString();
                using (IRedisClient redis = RedisClientPool.GetRedisClient())
                {
                    if (redis != null)
                    {
                        redis.SetValue(key, responseJson, TimeSpan.FromMinutes(10.0));
                    }
                }
                context.Response.Write(responseJson);
            }

        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("查询单位信息异常。{0}", err.Message));
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}